<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item
          v-for="(item, index) in breadList"
          :key="index"
          :to="{ path: item.path }"
        >{{ item.meta.title }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="120px"
          status-icon
        >
          <el-form-item label="订单号" prop="orderNo">
            <el-input v-model="ruleForm.orderNo" />
          </el-form-item>
          <el-form-item label="用户ID" prop="userId">
            <el-select v-model="ruleForm.userId" placeholder="请选择用户ID">
              <el-option
                v-for="(item) in userIdList"
                :key="item.userId"
                :label="item.userId"
                :value="item.userId"
              >
            </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="订单内容" prop="content">
            <el-input v-model="ruleForm.content" />
          </el-form-item>
          <el-form-item label="价格" prop="totalPrice">
            <el-input v-model="ruleForm.totalPrice" type="totalPrice" />
          </el-form-item>
          <el-form-item label="订单状态" prop="state">
            <el-radio-group v-model="ruleForm.state">
              <el-radio :label="0">未支付</el-radio>
              <el-radio :label="1">已支付</el-radio>
              <el-radio :label="2">已出票</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="完成时间" prop="endTime">
            <el-date-picker
              v-model="ruleForm.endTime"
              type="datetime"
              placeholder="选择日期时间"
            ></el-date-picker>
          </el-form-item>
          
        </el-form>
      </div>
      <div>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          新增
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">重置表单</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, reactive, watch, onMounted } from "vue";
import request from "../../utils/request";
import { ElNotification, ElSelect, ElOption } from "element-plus";

export default {
  setup() {
  
    let breadList = []; // 路由集合
    const userIdList = ref([]); // 用户ID列表
   
    // onMounted(async () => {
    //   await fetchUserIdList(); // 获取用户ID列表
    // });

    const fetchUserIdList = async () => {
      try {
        const response = await request.get("/user/getAllUsers"); // 从数据库获取用户ID列表的接口，根据实际情况修改路径
        if (response.data.code == 200) {
          userIdList.value = response.data.data;
        } else {
          ElNotification({
            title: "获取用户ID失败",
            message: "请联系管理员检查失败原因",
          });
        }
      } catch (error) {
        ElNotification({
          title: "获取用户ID失败",
          message: "请联系管理员检查失败原因",
        });
      }
    };
    fetchUserIdList();

    const resetForm = (formEl) => {
      if (!formEl) return;
      formEl.resetFields();
    };

    const submitForm = async (formEl) => {
      if (!formEl) return;
      await formEl.validate((valid, fields) => {
        if (valid) {
          request
            .request({
              url: "/order/add",
              method: "post",
              data: {
                orderNo: ruleForm.orderNo,
                content: ruleForm.content,
                totalPrice: ruleForm.totalPrice,
                state: ruleForm.state,
                endTime: ruleForm.endTime,
                userId: ruleForm.userId, // 提交用户ID字段
              },
            })
            .then((res) => {
              if (res.data.code == 200) {
                ElNotification({
                  title: "更新成功",
                  message: "成功更新订单信息",
                });
              } else {
                ElNotification({
                  title: "更新失败",
                  message: "请联系管理员检查失败原因",
                });
              }
            })
            .catch((e) => {
              ElNotification({
                title: "更新失败",
                message: "请联系管理员检查失败原因",
              });
            });
        } else {
          ElNotification({
            title: "校验失败",
            message: "请重新填写信息",
          });
        }
      });
    };

    const ruleForm = reactive({
      orderNo: "",
      content: "",
      totalPrice: "",
      state: 1,
      endTime: "",
      userId: "", // 新增的字段
    });

    const rules = reactive({
      orderNo: [
        {
          required: true,
          message: "请输入订单号",
          trigger: "blur",
        },
        { pattern: /^\d+(\.\d+)?$/, message: '订单号必须为数字', trigger: 'blur' }
      ],
      content: [
        {
          required: true,
          message: "订单内容",
          trigger: "blur",
        },
      ],
      totalPrice: [
        {
          required: true,
          message: "请输入订单价格",
          trigger: "blur",
        },
        { pattern: /^\d+(\.\d+)?$/, message: '订单价格必须为数字', trigger: 'blur' }
      ],
      state: [
        {
          required: false,
          message: "请选择订单状态",
          trigger: "blur",
        },
      ],
      endTime: [
        {
          required: true,
          message: "请选择完成时间",
          trigger: "change",
        },
      ],
      userId: [
        {
          required: true,
          message: "请选择用户ID",
          trigger: "change",
        },
      ],
    });

    const ruleFormRef = ref();

    return {
      breadList,
      resetForm,
      ruleFormRef,
      submitForm,
      ruleForm,
      rules,
      userIdList
    };
  },
};
</script>
  <style lang="scss" scoped>
  .handle-box {
    margin-bottom: 20px;
  }
  
  .handle-select {
    width: 120px;
  }
  
  .handle-input {
    width: 300px;
    display: inline-block;
  }
  
  .table {
    width: 100%;
    font-size: 14px;
  }
  
  .red {
    color: #ff0000;
  }
  
  .mr10 {
    margin-right: 10px;
  }
  
  .table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
  }
  </style>
    